/********************************************************
 * Filename: TclConfiguration.java
 * Author: LMU
 * Program: TUnit
 * Description: Creates the tcl configuration for eclipse.  
 * Revision History:
 * 20060418: Initial Revision.
 * *****************************************************/

package edu.lmu.cs.tUnit.editor;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.graphics.Color;

import edu.lmu.cs.tUnit.plugin.TUnitPlugin;
import edu.lmu.cs.tUnit.preferences.TclPreferencesConstants;

public class TclConfiguration extends SourceViewerConfiguration {
	private TclScanner scanner;
	private TclStringScanner stringScanner;
	private Color DEFAULT_COLOR; 
	private NonRuleBasedDamagerRepairer ndr;
	private ContentAssistant assistant;

	public TclConfiguration() {
	}
	
	public TclScanner getTclScanner() {
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		DEFAULT_COLOR = new Color(Display.getCurrent(), PreferenceConverter.getColor(store, TclPreferencesConstants.P_DEFAULTCOLOR));
		if(scanner == null) {
			scanner = new TclScanner();
			scanner.setDefaultReturnToken(new Token(new TextAttribute(DEFAULT_COLOR)));
		}
		return scanner;
	}
	
	public TclStringScanner getTclStringScanner() {
		if(stringScanner == null) {
			stringScanner = new TclStringScanner();
		}
		return stringScanner;
	}
	
	/**
	 * Returns the auto indentation strategy
	 */
	public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer isv, String ctype) {
		IAutoEditStrategy[] ret = {new TclAutoIndentStrategy()};
		return (ret);
	}
	
	/**
	 * Define the reconciler for the Tcl Editor
	 */
	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		Color cSTRING = new Color(Display.getCurrent(), PreferenceConverter.getColor(store, TclPreferencesConstants.P_STRINGCOLOR));

		PresentationReconciler reconciler = new PresentationReconciler();
		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getTclScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
		
		ndr = new NonRuleBasedDamagerRepairer(
				new TextAttribute(cSTRING));
		reconciler.setDamager(ndr, TclStringScanner.TCL_STRING);
		reconciler.setRepairer(ndr, TclStringScanner.TCL_STRING);
		
		return reconciler;
	}
	
	/**
	 * Define the Tcl Content Assist processor
	 */
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		assistant = new ContentAssistant();
		assistant.setContentAssistProcessor( new TclContentAssist(), IDocument.DEFAULT_CONTENT_TYPE);
		assistant.enableAutoActivation(store.getBoolean(TclPreferencesConstants.P_CONTENTASSIST));
		assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
		return assistant;
	}
	
	/**
	 * Updates the scanner according the prefernces
	 */
	public void updateScanner() {
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		Color cSTRING = new Color(Display.getCurrent(), PreferenceConverter.getColor(store, TclPreferencesConstants.P_STRINGCOLOR));
		getTclScanner().updateColors();
		ndr.changeColor(new TextAttribute(cSTRING));
	}
	
	/**
	 * Updates for content assistance
	 */
	public void updateContentAssist() {
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		assistant.enableAutoActivation(store.getBoolean(TclPreferencesConstants.P_CONTENTASSIST));
	}
}
